package com.jhj.common.interceptor;

import static com.jhj.common.constants.CookieConstants.CON_TYPE;
import static com.jhj.common.constants.CookieConstants.ID;
import static com.jhj.common.constants.CookieConstants.LOGIN_COOKIE_AGE;
import static com.jhj.common.constants.CookieConstants.LOGIN_REAL_NAME;
import static com.jhj.common.constants.CookieConstants.LOGIN_ROLE_CODE;
import static com.jhj.common.constants.CookieConstants.LOGIN_ROLE_NAME;
import static com.jhj.common.constants.CookieConstants.LOGIN_STATUS;
import static com.jhj.common.constants.CookieConstants.LOGIN_TIME;
import static com.jhj.common.constants.CookieConstants.LOGIN_TIME_FOMART;
import static com.jhj.common.constants.CookieConstants.LOGIN_USER_NAME;
import static com.jhj.common.constants.CookieConstants.LOGIN_USER_SITE;
import static com.jhj.common.constants.CookieConstants.OPERATOR;
import static com.jhj.common.constants.CookieConstants.REAL_NAME;
import static com.jhj.common.constants.CookieConstants.ROLE_CODE;
import static com.jhj.common.constants.CookieConstants.ROLE_NAME;
import static com.jhj.common.constants.CookieConstants.SYS_CON_TYPE;
import static com.jhj.common.constants.CookieConstants.SYS_GBT;
import static com.jhj.common.constants.CookieConstants.SYS_ID;
import static com.jhj.common.constants.CookieConstants.SYS_LOGIN_TIME;
import static com.jhj.common.constants.CookieConstants.SYS_REAL_NAME;
import static com.jhj.common.constants.CookieConstants.SYS_ROLE_CODE;
import static com.jhj.common.constants.CookieConstants.SYS_ROLE_NAME;
import static com.jhj.common.constants.CookieConstants.SYS_USER_PIN;
import static com.jhj.common.constants.CookieConstants.SYS_USER_SITE;
import static com.jhj.common.constants.CookieConstants.USER_PIN;
import static com.jhj.common.constants.CookieConstants.USER_SITE;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.jhj.common.util.JhjCookieUtil;
import com.jhj.common.util.JhjEncryptUtil;
import com.jhj.common.util.JhjTimeUtil;

public class JhjManCookieInterceptor extends HandlerInterceptorAdapter {
    
    private String secretKey;
    
    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }
    
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        
        String id = JhjCookieUtil.getValue(SYS_ID, request);
        String userPin = JhjCookieUtil.getValue(SYS_USER_PIN, request);
        String realName = JhjCookieUtil.getValue(SYS_REAL_NAME, request);
        /** 用户类型 1-商家 2-会员 3-运营用户 */
        String conType = JhjCookieUtil.getValue(SYS_CON_TYPE, request);
        String loginTime = JhjCookieUtil.getValue(SYS_LOGIN_TIME, request);
        String roleName = JhjCookieUtil.getValue(SYS_ROLE_NAME, request);
        String roleCode = JhjCookieUtil.getValue(SYS_ROLE_CODE, request);
        String userSite = JhjCookieUtil.getValue(SYS_USER_SITE, request);
        /** 登陆成功后加密后的数据字符串 */
        String gbt = JhjCookieUtil.getValue(SYS_GBT, request);
        
        Map<String, String> paramsMap = new HashMap<String, String>();
        paramsMap.put(ID, id);
        paramsMap.put(USER_PIN, userPin);
        paramsMap.put(REAL_NAME, realName);
        paramsMap.put(CON_TYPE, conType);
        paramsMap.put(LOGIN_TIME, loginTime);
        paramsMap.put(ROLE_NAME, roleName);
        paramsMap.put(ROLE_CODE, roleCode);
        paramsMap.put(USER_SITE, userSite);
        
        String sign = JhjEncryptUtil.getSign(paramsMap, secretKey);
        int day = Integer.MAX_VALUE;
        
        JhjTimeUtil shopTimeUtil = JhjTimeUtil.getInstance();
        Date loginDateTime = shopTimeUtil.getDateFromString(loginTime, LOGIN_TIME_FOMART);
        
        if (loginDateTime != null) {
            day = shopTimeUtil.intervalDays(loginDateTime, new Date());
        }
        
        // 加密字符串一致，且为三天之内输入密码登录成功, 且用户类型为运营用户
        if (sign.equals(gbt) && day <= LOGIN_COOKIE_AGE && OPERATOR.equals(conType)) {
            // 登录用户
            request.setAttribute(LOGIN_STATUS, true);
            request.setAttribute(LOGIN_USER_NAME, userPin);
            request.setAttribute(LOGIN_REAL_NAME, realName);
            request.setAttribute(LOGIN_ROLE_NAME, roleName);
            request.setAttribute(LOGIN_ROLE_CODE, roleCode);
            request.setAttribute(LOGIN_USER_SITE, userSite);
        } else {
            // 未登录
            request.setAttribute(LOGIN_STATUS, false);
        }
        
        return super.preHandle(request, response, handler);
    }
}
